//
//  HUDSquareVC.swift
//  BWPanelKit
//
//  Created by zhuxuhong on 2022/3/7.
//

import BWUIKit
import UIKit

class HUDSquareVC: HUDVC {
    static let iconXmark = UIImage.xmark(iconSize: .bw_with(both: 14), imageSize: .bw_with(both: 20), lineWidth: 1)
    
    @IBOutlet weak var widthCons: NSLayoutConstraint!
    @IBOutlet weak var heightCons: NSLayoutConstraint!
    
    @IBOutlet weak var cancelBtn: UIButton!{
        didSet{
            cancelBtn.setImage(Self.iconXmark, for: .normal)
            cancelBtn.backgroundColor = .clear
        }
    }
    @IBOutlet weak var indicatorView: HUDIndicatorView!
    @IBOutlet weak var progressView: HUDProgressView!
    
    private var square: HUD.Square? {
        return hud as? HUD.Square
    }
    
    func updateProgress(_ value: CGFloat, text: String?) {
        square?.progress = max(0, min(1, value))
        
        guard let square = square,
              case .progress(let type) = square.type else {
            return
        }
        indicatorView.isHidden = true
        imageView.isHidden = true
        progressView.isHidden = false
        customWrapperView.isHidden = true
        
        progressView.type = type
        progressView.value = value
        textLabel.text = text
        textLabel.isHidden = text == nil
    }
    
    override func setupStyles() {
        super.setupStyles()
        
        if let _ = square?.canceller {
            widthCons.constant = 120
            heightCons.constant = 120
            cancelBtn.isHidden = false
        }
        view.backgroundColor = square?.maskColor
    }
    
    @IBAction func cancelBtnTapped(_ sender: UIControl) {
        square?.canceller?()
    }
    
    override func setupHUDView() {
        super.setupHUDView()
        
        textLabel.textAlignment = .center
        
        switch (square?.type) {
        case .tips(let type):
            indicatorView.isHidden = true
            imageView.isHidden = false
            progressView.isHidden = true
            customWrapperView.isHidden = true
            imageView.image = type.icon
            
        case .loading(let type):
            indicatorView.type = type
            indicatorView.isHidden = false
            imageView.isHidden = true
            progressView.isHidden = true
            customWrapperView.isHidden = true
            
        case .progress:
            updateProgress(square?.progress ?? 0, text: hud.text)
            
        case .custom:
            indicatorView.isHidden = true
            imageView.isHidden = true
            progressView.isHidden = true
            customWrapperView.isHidden = false
            
        case .none: break
        }
    }
}
